package com.shanyin.framework.member.web;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.shanyin.framework.common.base.RedissLock;
import com.shanyin.framework.common.model.SysConfig;
import com.shanyin.framework.common.sys.SysConfigService;
import com.shanyin.framework.common.util.RedissLockUtil;
import com.shanyin.framework.member.entity.SysDictDetail;
import com.shanyin.framework.member.service.SysDictDetailService;

/***
 ** @category 请用一句话来描述其用途...
 ** @author qing.yunhui
 ** @email: 280672161@qq.com
 ** @createTime: 2018年5月25日下午2:42:49
 **/
@RestController
@RequestMapping(TestController.ACTION_PATH)
public class TestController {
	
    final Logger logger = LoggerFactory.getLogger(getClass());
	
    final static String ACTION_PATH="/test";

    @Autowired
	private SysDictDetailService sysDictDetailService;
    
    @Autowired
	private SysConfigService sysConfigService;
    
    @Autowired  
    RedissLockUtil redissLockUtil;
    
    @GetMapping("/queryAllConfig")
    public List<SysConfig> queryAll(){
    	logger.info("====================>test，接到的请求,准备查询后台配置信息...");
    	List<SysConfig> configList=sysConfigService.query(null);
    	logger.info("====================>test，请求已执行,查询到的配置信息有:{}.",new Object[] {JSONObject.toJSONString(configList)});
    	return configList;
    }
    
	@GetMapping("/{itemKey}")
	public List<SysDictDetail> findOrderById(@PathVariable String itemKey) {
		logger.info("====================>test，接收到的请求参数:itemKey={}",new Object[]{itemKey});
		List<SysDictDetail> dictDetailList =null;
		RedissLock redissLock=new RedissLock("testlock","lockvalue");  
		logger.info("====================>test，准备加锁:加锁的key={}",new Object[]{JSONObject.toJSONString(redissLock)});
		if(redissLockUtil.tryLock(redissLock)){  
			try {
				logger.info("====================>test，加锁完毕:加锁的key={}，加锁的时间为:{}分钟.",new Object[]{JSONObject.toJSONString(redissLock),5});
				logger.info("==========>itemKey={}.",new Object[] {itemKey});
				dictDetailList = sysDictDetailService.querySysDictDetailByItemKey(itemKey);
				logger.info("==========>dictDetailList={}.",new Object[] {JSONObject.toJSONString(dictDetailList)});
				try {
					Thread.sleep(15000);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} finally {
				logger.info("====================>test，开始对key={}的锁进行释放.",new Object[]{JSONObject.toJSONString(redissLock)});
				redissLockUtil.releaseLock(redissLock);  
				logger.info("====================>test，key={}的锁释放成功.",new Object[]{JSONObject.toJSONString(redissLock)});
			}
        }  
		return dictDetailList;
	}
}
